<?php
/**
 * @title 2779. 数组的最大美丽值 - 二分查找
 * @author start2004
 */

// ini_set("memory_limit", "128M");

// class Solution {
//
//     function find() {
//
//     }
// }

class Solution {

    /**
     * @param Integer[] $nums
     * @param Integer $k
     * @return Integer
     */
    function maximumBeauty($nums, $k) {
        /**
         * @since 2024-01-31 排序nums
         */
        sort($nums);

        /**
         * @since 2024-01-31 nums变全局变量
         * @since 2024-01-31 nums的元素个数
         * @since 2024-01-31 nums最大值和最小值
         */
        $this->nums = $nums;
        $numsCount = count($nums);
        $min = $nums[0];
        $max = $nums[$numsCount-1];

        /**
         * @since 2024-01-31 返回数组可能取得的 最大 美丽值
         */
        $maxCount = 0;

        /**
         * @since 2024-01-31 遍历[min, max]每个数
         */
        for($i=$min; $i<=$max; $i++){
            /**
             * @since 2024-01-31 [i-k, i+k]范围内，有多少个元素
             */
            $count1 = $this->FindCount($i-$k-1);
            $count2 = $this->FindCount($i+$k);
            $maxCount = max($maxCount, $count2-$count1);
        }

        /**
         * @return Integer
         */
        return $maxCount;
    }

    /**
     * @title 返回nums中，<=targetNum的数量
     * @author start2004
     * @since 2024-01-31 Created
     *
     * @param Integer $targetNum
     */
    function FindCount($targetNum){
        /**
         * @since 2024-01-31 二分查找
         */
        $s = 0;
        $e = count($this->nums)-1;

        while($s<=$e){
            $m = ($s+$e)>>1;
            $v = $this->nums[$m];

            if($v<=$targetNum){
                $s = $m+1;
            } elseif($v > $targetNum){
                $e = $m-1;
            } else {}
        }

        /**
         * @return Integer
         */
        return $s;
    }
}



/**
 * @url http://127.0.0.1/leetcode/202401/2024.01.31_2.php
 */
$datas = [
    [[4,6,1,2], 2],
];

include_once dirname(__DIR__) . DIRECTORY_SEPARATOR ."xhprof.php";
$xhprof = new Xhprof();
foreach ($datas as $data){
    /**
     * @since 2024-01-15 超时和错误案例参数
     */
    // $data = $xhprof->FormatArgs("/tmp/leetcode/xxx-xxx.txt");

    // $data[0] = $xhprof->listNode($data[0]);
    // $data[0] = $xhprof->tree($data[0]);
    echo $xhprof->DumpArgs($data);
    echo "\n<hr>\n";

    $obj = new Solution();
    $result = $obj->maximumBeauty(...$data);
    echo $xhprof->DumpArgs([$result]);
    // var_dump($result);

    echo "\n<hr>". str_repeat("<br>", 2) . str_repeat(PHP_EOL, 5);
}

// foreach ($datas as $data){
//     $obj = new $data[0][0](...$data[1][0]);
//
//     for ($i=1; $i<count($data[0]); $i++){
//         $func = $data[0][$i];
//         $param = $data[1][$i];
//         $result = $obj->$func(...$param);
//
//         echo $data[0][$i] ."(\"". implode(",", $data[1][$i]) ."\") ";
//             if($result === true){
//                 echo "True";
//             } elseif($result === false) {
//                 echo "False";
//             } elseif($result === null) {
//                 echo "Null";
//             } elseif(is_array($result)) {
//                 var_dump($result);
//             } else {
//                 echo $result;
//             }
//         echo PHP_EOL;
//     }
//
//     echo str_repeat(PHP_EOL, 3);
// }
$xhprof->end();
